<template>
  <el-container>
    <el-form :model="form"
             ref="form"
             :rules="rule"
             label-position="right"
             label-width="150px">
      <el-form-item label="图片:"
                    prop="imageUrl">
        <el-upload class="avatar-uploader"
                   :action="upload_url"
                   :show-file-list="false"
                   :on-success="uploadSuccess"
                   :before-upload="beforeUpload"
                   :headers="header"
                   name="image">
          <img v-if="form.imageUrl"
               :src="form.imageUrl"
               class="avatar" />
          <i v-else
             class="el-icon-plus avatar-uploader-icon"></i>
          <div class="el-upload__tip"
               slot="tip">图片大小不能超过2M </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="跳转类型:"
                    prop="position_id">
        <el-select v-model.number="form.redirect_type"
                   placeholder="请选择">
          <el-option v-for="row in redirect_type"
                     :key="row"
                     :value="row"
                     :label="row"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="跳转链接:"
                    prop="url">
        <el-input type="text"
                  v-model="form.url"
                  placeholder="请输入跳转链接"></el-input>
      </el-form-item>
      <el-form-item label="上线时间:"
                    prop="start_date">
        <el-date-picker v-model="form.start_date"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="下线时间:"
                    prop="end_date">
        <el-date-picker v-model="form.end_date"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="排序:"
                    prop="sort">
        <el-input type="number"
                  v-model.number="form.sort"
                  min="0"
                  style="width: 20%;"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="success"
                   icon="el-icon-edit"
                   @click.prevent="addBanner"
                   :loading="loading">编辑</el-button>
      </el-form-item>
    </el-form>
  </el-container>
</template>

<script>
import { bannerType, updBanner, bannerInfo } from '@/api/modules/appConfig'
import { STORAGE } from '@/components/common/global'
export default {
  name: "edit",
  data () {
    return {
      upload_url: process.env.VUE_APP_API_BASE_URL + '/api/tool/upload/image',
      loading: false,
      banner_id: 0,
      form: {
        imageUrl: '',
        position_id: '',
        url: '',
        end_date: '',
        sort: 0,
        start_date: ''
      },
      //轮播图位置列表
      position: [],
      redirect_type: [
        'webview', 'native'
      ],
      rule: {
        imageUrl: [
          { required: true, message: '请上传图片信息' }
        ],
        position_id: [
          { required: true, message: '位置信息必选', trigger: 'blur' },
        ],
        start_date: [
          { required: true, message: '上线时间必选', trigger: 'blur' },
        ],
        end_date: [
          { required: true, message: '下线时间必选', trigger: 'blur' },
        ],
        sort: [
          { required: true, message: '排序权重必选', trigger: 'blur' },
        ]
      },
      header: {
        'api-token': localStorage.getItem(STORAGE.API_TOKEN)
      },
    };
  },
  methods: {
    uploadSuccess (response, file, fileList) {
      if (response.status == 'success') {
        this.form.imageUrl = response.data;
      } else {
        this.$notify.error(response.failedMsg);
      }
    },
    beforeUpload (file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isLt2M;
    },
    reset (ref) {
      this.$refs[ref].resetFields();
    },
    addBanner () {
      var data = {
        id: this.banner_id,
        images: this.form.imageUrl,
        redirect_type: this.form.redirect_type,  //这里是地址的id
        uptime: this.form.start_date,
        downtime: this.form.end_date,
        sort: this.form.sort
      };

      if (this.form.url.trim().length != 0 && this.form.url.trim() != '') {
        data.url = this.form.url;
      }
      this.loading = true;
      updBanner(data).then(response => {
        this.loading = false;
        if (response.data.status !== 'failed') {
          this.$message.success('修改成功');
          bannerInfo({ id: this.banner_id }).then(response => {
            this.form.imageUrl = response.data.images;
            this.form.redirect_type = response.data.redirect_type;
            this.form.start_date = response.data.uptime;
            this.form.end_date = response.data.downtime;
            this.form.sort = response.data.sort;
            this.form.url = response.data.url;
          });
        }
      }).catch(err => {
        this.loading = false;
      })
    }
  },
  mounted () {
    this.banner_id = this.$route.query.banner_id;
    bannerInfo({ id: this.banner_id }).then(response => {
      this.form.imageUrl = response.data.images;
      this.form.redirect_type = response.data.redirect_type;
      this.form.start_date = response.data.uptime;
      this.form.end_date = response.data.downtime;
      this.form.sort = response.data.sort;
      this.form.url = response.data.url;
    });
  }
}
</script>

<style scoped>
.el-container {
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  border: 1px solid #f2f6fc;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  margin: 10px auto;
  background-color: #fff;
  padding: 20px;
}

.el-header h3 {
  height: 30px;
  text-align: center;
  font-size: 20px;
  line-height: 30px;
  color: gray;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 375px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 100%;
  height: 100%;
}
.el-upload {
  width: auto;
}
</style>